package io.renren.modules.carpicture.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mchange.v1.util.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;



import io.renren.modules.carpicture.dao.CarPictureDao;
import io.renren.modules.carpicture.entity.CarPictureEntity;
import io.renren.modules.carpicture.service.CarPictureService;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;


@Service("carPictureService")
@Slf4j
public class CarPictureServiceImpl extends ServiceImpl<CarPictureDao, CarPictureEntity> implements CarPictureService {

    @Override
    public void savePictures(List<String> picture, Long carInfoId) {
        log.info("保存车辆信息图片");
        for (String s : picture) {
            CarPictureEntity carPictureEntity = new CarPictureEntity();
            carPictureEntity.setCarInfoId(carInfoId);
            carPictureEntity.setPicture(s);
            this.baseMapper.insert(carPictureEntity);
        }
    }

    @Override
    public List<String> getByCarInfoId(Long id) {
        log.info("根据车辆信息表id获取车辆图片");
        QueryWrapper<CarPictureEntity> wrapper = new QueryWrapper<CarPictureEntity>();
        wrapper.eq("car_info_id",id);
        List<CarPictureEntity> carPictureEntities = this.baseMapper.selectList(wrapper);
        if (CollectionUtil.isNotEmpty(carPictureEntities)) {
            List<String> picture = carPictureEntities.stream().map(e -> e.getPicture()).collect(Collectors.toList());
            return picture;
        }
        return null;
    }

    @Override
    public void deleteBy(Long carInfoId) {
        log.info("根据车辆信息表id删除原有数据");
        QueryWrapper<CarPictureEntity> wrapper = new QueryWrapper<CarPictureEntity>();
        wrapper.eq("car_info_id",carInfoId);
        this.baseMapper.delete(wrapper);
    }
}
